package com.xc.erp.modules.erp.controller.depot;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xc.erp.common.annotation.Log;
import com.xc.erp.modules.erp.domain.pojo.JshDepotHead;
import com.xc.erp.modules.erp.domain.pojo.JshDepotItem;
import com.xc.erp.modules.erp.domain.pojo.MaterialExtend;
import com.xc.erp.modules.erp.domain.vo.ResultDepotMaterialVo;
import com.xc.erp.modules.erp.service.*;
import com.xc.erp.modules.erp.domain.AjaxResult;
import com.xc.erp.modules.erp.domain.vo.DepotConditionVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;


@RestController
@RequestMapping("/depotHead")
public class DepotHeadController {
    @Autowired
    private JshDepotHeadService jshDepotHeadService;

    @Autowired
    private JshMaterialExtendService jshMaterialExtendService;
    @Autowired
    private JshDepotItemService jshDepotItemService;
    @Autowired
    private JshMaterialService jshMaterialService;
    @GetMapping("/getList")
    public AjaxResult getList(Long headerId) {
        JshDepotHead info = jshDepotHeadService.getOne(new QueryWrapper<JshDepotHead>().eq("id", headerId));
        List<JshDepotItem> list = jshDepotItemService.list(new QueryWrapper<JshDepotItem>().eq("header_id",headerId));
        List<Long> materialExtendIds=new ArrayList<>();
        for (JshDepotItem findDepotVo : list) {
            materialExtendIds.add(findDepotVo.getMaterialExtendId());
        }
        List<String> MbarCodes=new ArrayList<>();
        for (Long materialExtendId : materialExtendIds) {
            MbarCodes.add(jshMaterialExtendService.getOne(new QueryWrapper<MaterialExtend>().eq("id",materialExtendId)).getBarCode());
        }
        ArrayList<ResultDepotMaterialVo> jshDepotItems = new ArrayList<>();
        for (String mbarCode : MbarCodes) {
          jshDepotItems.add(jshMaterialService.getMaterialByBarCode(mbarCode, headerId));
        }
        for (int i = 0; i < list.size(); i++) {
            if(list.get(i).getAnotherDepotId()!=null)
                jshDepotItems.get(i).setAnotherDepotId(list.get(i).getAnotherDepotId());
            jshDepotItems.get(i).setOperNumber(list.get(i).getOperNumber());
            jshDepotItems.get(i).setDepotId(list.get(i).getDepotId());
            jshDepotItems.get(i).setBillPrice(list.get(i).getOperNumber().multiply(jshDepotItems.get(i).getUnitPrice()));
        }
        return AjaxResult.success().put("info",info).put("jshDepotItemList",jshDepotItems);
    }
    @GetMapping("/list")
    public AjaxResult list(DepotConditionVo conditionVo) {
        Page<JshDepotHead> depotHeadPage = jshDepotHeadService.jshDepotHeadList(conditionVo);
        return AjaxResult.success().put("rows", depotHeadPage.getRecords()).put("total", depotHeadPage.getTotal());
    }
    @Log(modul = "单据",desc = "新增单据")
    @PostMapping("/addDepotHeadAndDetail")
    public AjaxResult addDepotHeadAndDetail(@RequestBody JshDepotHead info) {
        if (jshDepotHeadService.addDepotHeadAndDetail(info)) {
            return AjaxResult.success();
        } else {
            return AjaxResult.error();
        }
    }
    @Log(modul = "单据",desc = "修改单据")
    @PutMapping("/updateDepotHeadAndDetail")
    public AjaxResult updateDepotHeadAndDetail(@RequestBody JshDepotHead info){
        if (jshDepotHeadService.updateDepotHeadAndDetail(info)) {
            return AjaxResult.success();
        } else {
            return AjaxResult.error();
        }
    }

    @Autowired
    private JshMaterialCurrentStockService jshMaterialCurrentStockService;
    @Log(modul = "单据",desc = "删除单据")
    @DeleteMapping("/deleteBatch")
    public AjaxResult delIds(String ids) {
        String id=ids.substring(1,ids.length()-1);
        String[] split = id.split(",");
        long count = jshDepotHeadService.count(new QueryWrapper<JshDepotHead>().in("id", split).eq("status", "0"));
        if (count > 0) {
            return new AjaxResult(8500011, "抱歉，只有未审核的单据才能删除");
        }
        boolean b = jshDepotHeadService.delIds(split);
        if (b)
            return AjaxResult.success();
        return AjaxResult.error();
    }
    @Log(modul = "单据",desc = "修改单据状态")
    @PutMapping("/batchSetStatus")
    public AjaxResult batchSetStatus(@RequestParam(value = "ids") String ids, String status) {
        String id=ids.substring(1,ids.length()-1);
        String[] split = id.split(",");
        QueryWrapper<JshDepotHead> queryWrapper = new QueryWrapper<JshDepotHead>().in("id", split);
        List<JshDepotHead> list = jshDepotHeadService.list(queryWrapper);
        for(JshDepotHead jshDepotHead:list){
            if(jshDepotHead.getStatus().equals(status)){
                if ("0".equals(status))
                    return new AjaxResult(8500013, "抱歉，只有已审核的单据才能反审核");
                if ("1".equals(status))
                    return new AjaxResult(8500012, "抱歉，只未审核的单据才能审核");
            };
        }
        if (jshDepotHeadService.updateBatchSetStatus(split, status)) {
            return AjaxResult.success();
        }
        return AjaxResult.error();
    }
}
